// +----------------------------------------------------------------------
// | JavaWeb前后端分离框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2022 小菜工作室
// +----------------------------------------------------------------------
// | 官方网站: http://www.xiaocai.work/
// +----------------------------------------------------------------------
// | 作者: 小菜 <414797210@qq.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

package com.javaweb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.api.entity.MemberMenu;
import com.javaweb.api.mapper.MemberMenuMapper;
import com.javaweb.api.service.IMemberMenuService;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员菜单表 服务实现类
 * </p>
 *
 * @author 鲲鹏
 * @since 2020-10-30
 */
@Service
public class MemberMenuServiceImpl extends BaseServiceImpl<MemberMenuMapper, MemberMenu> implements IMemberMenuService {

    @Autowired
    private MemberMenuMapper memberMenuMapper;


    /**
     * 获取导航菜单
     *
     * @param groupId 分组ID
     * @return
     */
    @Override
    public List<MemberMenu> getMemberMenuList(Integer groupId) {
        List<MemberMenu> menuList = null;
        if (groupId.equals(1)) {
            menuList = getChildrenMenuAll(0);
        } else {
            menuList = memberMenuMapper.getPermissionsListByGroupId(groupId, 0);
            if (!StringUtils.isNull(menuList)) {
                for (MemberMenu menu : menuList) {
                    List<MemberMenu> childrenList = getChildrenMenuByPid(groupId, menu.getId());
                    menu.setChildren(childrenList);
                }
            }
        }
        return menuList;
    }

    /**
     * 根据父级ID获取子级菜单
     *
     * @param pid 上级ID
     * @return
     */
    public List<MemberMenu> getChildrenMenuAll(Integer pid) {
        QueryWrapper<MemberMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        queryWrapper.eq("status", 1);
        // 只取菜单一级
        queryWrapper.eq("type", 0);
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");
        List<MemberMenu> menuList = list(queryWrapper);
        if (!menuList.isEmpty()) {
            menuList.forEach(item -> {
                List<MemberMenu> childrenList = getChildrenMenuAll(item.getId());
                item.setChildren(childrenList);
            });
        }
        return menuList;
    }

    /**
     * 根据上级ID获取子级菜单
     *
     * @param groupId 分组ID
     * @param pid    上级ID
     * @return
     */
    public List<MemberMenu> getChildrenMenuByPid(Integer groupId, Integer pid) {
        List<MemberMenu> menuList = memberMenuMapper.getPermissionsListByGroupId(groupId, pid);
        if (!menuList.isEmpty()) {
            menuList.forEach(item -> {
                List<MemberMenu> childrenList = getChildrenMenuByPid(groupId, item.getId());
                item.setChildren(childrenList);
            });
        }
        return menuList;
    }

    /**
     * 获取所有菜单列表
     *
     * @return
     */
    @Override
    public List<MemberMenu> getMemberMenuAll() {
        QueryWrapper<MemberMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.le("type", 2);
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByAsc("sort");
        List<MemberMenu> menuList = list(queryWrapper);
        return menuList;
    }

    /**
     * 获取权限节点列表
     *
     * @param groupId 用户ID
     * @return
     */
    @Override
    public List<String> getPermissionList(Integer groupId) {
        List<String> permissionList = new ArrayList<>();
        List<String> stringList = memberMenuMapper.getPermissionList(groupId);
        if (!stringList.isEmpty()) {
            for (String permission : stringList) {
                permissionList.add(permission);
            }
        }
        return permissionList;
    }
}
